package com.hu.zhcc.shiro.controller.HealthIntervention;

import com.hu.zhcc.common.entity.Page;
import com.hu.zhcc.shiro.entity.bo.Member;
import com.hu.zhcc.shiro.entity.dto.HealthIntervention.PopulationDTO;
import com.hu.zhcc.shiro.entity.vo.PopulateDetailVO;
import com.hu.zhcc.shiro.service.HealthIntervention.PopulationclassificationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/population")
public class PopulationClassificationController {
    @Autowired
    public PopulationclassificationService service;

    //初始查询
    @GetMapping("")
    public ResponseEntity<Page<Member>> findPopList(String name, int offset, int limit){
        Map map = new HashMap();
        if(!StringUtils.isBlank(name)){
            map.put("name",name);
        }
        Page<Member> memberPage = service.selectByPopList(map,offset, limit);
        return ResponseEntity.ok(memberPage);
    }

    //根据编号删除信息
    @DeleteMapping("/{id}")
    public int removePopulation(@PathVariable("id") int id){
        return service.deleteByPrimaryKey(id);
    }

    //根据编号查询
    @GetMapping("/{id}")
    public ResponseEntity<PopulateDetailVO> getPopulation(@PathVariable("id") int id){
        PopulationDTO dto = service.selectByPrimaryKey(id);
        if(dto != null){
            PopulateDetailVO vo = new PopulateDetailVO();
            BeanUtils.copyProperties(dto,vo);
            return ResponseEntity.ok(vo);
        }
        return ResponseEntity.notFound().build();
    }

    //根据编号查询
    @GetMapping("getPopualtionById/{id}")
    public ResponseEntity<Member> getPopualtionById(@PathVariable("id") int id){
        return ResponseEntity.ok(service.findPopulation(id));
    }

    //修改信息和分类
    @PutMapping("/upPopulation")
    public ResponseEntity<Integer> updatePopulation(@RequestBody PopulateDetailVO vo){
        PopulationDTO dto = new PopulationDTO();
        BeanUtils.copyProperties(vo,dto);
        int i = service.updateByPopulationDTO(dto);
        if(i == 0){
            //返回一个404
            return  ResponseEntity.notFound().build();
        }
        return ResponseEntity.status(HttpStatus.CREATED).body(i);
    }
}
